package org.zhenglide.oa.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.repository.ProcessDefinition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.zhenglide.oa.service.IDeployService;
@Service
public class DeployServiceImpl implements IDeployService {
	
	@Autowired
	RepositoryService repositoryService;
	
	
   public List<Deployment> getDeploymentList() {
	   List<Deployment> deploymentList = repositoryService.createDeploymentQuery()
			   .orderByDeploymenTime().asc().list();
	   return deploymentList;
   }
   public void deleteProcessDefinitionByDeploymentId(String developmentId){ 
	    repositoryService.deleteDeployment(developmentId,true);
   }

	@Override
	public void deployZip(File file ,String filename) throws FileNotFoundException {
		// TODO Auto-generated method stub
		DeploymentBuilder builder = repositoryService.createDeployment();
		FileInputStream fileInputStream = new FileInputStream(file);
		ZipInputStream zipInputStream =new ZipInputStream(fileInputStream);
		builder.addZipInputStream(zipInputStream).name(filename).deploy();
	}


	@Override
	public List<ProcessDefinition> getProcessDefineList() {
		// TODO Auto-generated method stub
		List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().orderByProcessDefinitionVersion().asc().list();
		return list;
	}


	@Override
	public InputStream findImageInputStream(String deploymentId, String imageName) {
		// TODO Auto-generated method stub
		return repositoryService.getResourceAsStream(deploymentId, imageName);
	}
   
}
